<template>
  <div class="pic_contain">
    <div class="pic_title">
      <div class="title_text">拖动下方滑块完成拼图</div>
      <i class="el-icon-close" @click="closeModel"></i>
    </div>
    <div class="bg_pic" ref="bgImg">
      <img :src="originalImage" @load="loadImage" />
      <div class="slider_img" ref="sliderImg" :style="{ left: sliderLeft }">
        <img :src="slidingImage" />
      </div>
    </div>
    <div class="slide_verify_slider" :style="{height:sliderImgWidth+'px',lineHeight:sliderImgWidth+'px'}">
      <div class="slider_mask" :style="{width:sliderMaskWidth,height:sliderImgWidth+'px'}">
        <div class="slider_mask_item" ref="sliderMaskItem" @mousedown="handleStartEvent" @mouseup="handleEndEvent" @mousemove="handleMoveEvent" @mouseleave="mouseLeaveEvent" :style="{ left: sliderLeft,height:sliderImgWidth+'px',width:sliderImgWidth +'px'}">
          <i class="iconfont icon-youjiantou"></i>
        </div>
      </div>
      <span>向右滑动验证</span>
    </div>
  </div>
</template>

<script>
import { getVerifyImage, getVerifyImageAdmin } from '@/utils/api'
export default {
  props: {
    loginFlag: {
      type: Number,
      required: true,
    }
  },
  data() {
    return {
      slidingImage: '',//滑块图
      originalImage: '',//背景图
      uniqueId: '',//唯一id
      xwidth: '',
      yheight: '',
      bgWidth: 0,
      sliderImgWidth: 0,
      sliderMaskWidth: 0,
      sliderLeft: 0,
      draging: false,
      posX: 0,//元素初始的位置
      offsetX: 0,//元素偏离的位置
    }
  },
  mounted() {
    document.addEventListener('mousemove', this.handleMoveEvent);
    document.addEventListener('mouseup', this.handleStartEvent);
    this.loadPic()
  },
  methods: {
    handleStartEvent(e) {
      this.draging = true
      this.offsetX = e.clientX - this.posX
    },
    handleMoveEvent(e) {
      if (this.draging) {
        this.posX = e.clientX - this.offsetX
        if (this.posX <= 0) {
          this.posX = 0
        }
        if (this.posX >= (this.bgWidth - this.sliderImgWidth)) {
          this.posX = this.bgWidth - this.sliderImgWidth
        }
        this.sliderMaskWidth = this.posX + 'px'
        this.sliderLeft = this.posX + 'px'
      }
    },
    handleEndEvent(e) {
      this.draging = false
      let param = {
        uniqueId: this.uniqueId,//唯一id
        xwidth: this.posX,
        yheight: this.yheight,
      }
      this.$emit('onSuccess', param)
    },
    mouseLeaveEvent(e) {
      if (this.draging) {
        this.$emit('onSuccess', '')
        this.messageWarnPop('请勿离开滑动区域,请重试')
      }
    },
    closeModel() {
      this.$emit('onSuccess', '')
    },
    loadPic() {
      if (this.loginFlag == 1) {
        getVerifyImageAdmin().then(res => {
          this.slidingImage = 'data:image/jpg;base64,' + res.data.slidingImage
          this.originalImage = 'data:image/jpg;base64,' + res.data.originalImage
          this.uniqueId = res.data.uniqueId
          this.xwidth = res.data.xwidth
          this.yheight = res.data.yheight
          this.$refs.sliderImg.style.top = res.data.yheight + 'px'
        })
      } else {
        getVerifyImage().then(res => {
          this.slidingImage = 'data:image/jpg;base64,' + res.data.slidingImage
          this.originalImage = 'data:image/jpg;base64,' + res.data.originalImage
          this.uniqueId = res.data.uniqueId
          this.xwidth = res.data.xwidth
          this.yheight = res.data.yheight
          this.$refs.sliderImg.style.top = res.data.yheight + 'px'
        })
      }
    },
    loadImage() {
      this.$nextTick(() => {
        this.bgWidth = this.$refs.bgImg.offsetWidth;
        this.sliderImgWidth = this.$refs.sliderImg.offsetWidth;
      });
    },
  },
  beforeDestroy() {
    document.removeEventListener('mousemove', this.handleMoveEvent);
    document.removeEventListener('mouseup', this.handleStartEvent);
  }
}
</script>

<style lang="scss" scoped>
.pic_contain {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  background: #fff;
  border-radius: 6px;
  padding: 5px 10px 20px 10px;
  .pic_title {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    i {
      font-weight: bold;
      font-size: 20px;
    }
    .title_text {
      padding: 5px;
      font-size: 18px;
      font-weight: bold;
    }
  }
  .bg_pic {
    width: 100%;
    position: relative;
    .slider_img {
      top: 0;
      z-index: 10;
      position: absolute;
    }
  }
  .slide_verify_slider {
    position: relative;
    width: 100%;
    height: 40px;
    margin-top: 15px;
    line-height: 40px;
    text-align: center;
    background: #f7f9fa;
    color: #45494c;
    border: 1px solid #e4e7eb;
    .slider_mask {
      position: absolute;
      left: 0;
      top: 0;
      height: 40px;
      background: #d1e9fe;
      .slider_mask_item {
        position: absolute;
        left: 0;
        top: 0;
        height: 40px;
        width: 40px;
        box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);
        cursor: pointer;
        background: #fff;
        i {
          font-size: 22px;
        }
      }
    }
  }
}
</style>
